package projectmanagement.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import projectmanagement.model.Person;
import projectmanagement.model.dao.PersonDao;

public class PersonController extends SimpleFormController{
	public static final String PERSON_ID = "id";
	
	private PersonDao personManager;

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
		ModelAndView modelAndView = new ModelAndView("person");

		String id = request.getParameter(PERSON_ID);

		if (id != null && id.trim().length() > 0) {
			Person person = this.personManager.getPersonById(Integer.parseInt(id));
			
			if (person != null){
				modelAndView.addObject("person", person);
			}else{
				throw new ServletException("Nėra tokio darbuotojo.");
			}
		} else {
			throw new ServletException("Blogai nurodytas id.");
		}

		return modelAndView;
    }

	public PersonDao getPersonManager() {
		return personManager;
	}

	public void setPersonManager(PersonDao personManager) {
		this.personManager = personManager;
	}

}
